package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mysql.cj.Session;
import com.project.bean.UserBean;
import com.project.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("user")
@Api(tags = "用户控制器")
public class UserController {
    @Autowired
    private IUserService service;


    @RequestMapping("login")
    @ApiOperation(value = "用户登录", notes = "用户登录")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userName", value = "用户名"),
            @ApiImplicitParam(name = "pwd", value = "密码")
    }
    )
    public String login(String userName, String pwd, HttpServletRequest req) {
//        UserBean user = service.login(userName, pwd);
//        if (user != null) {
//            req.getSession().setAttribute("loginUser", user);
//            return "ok";
//        }
//        return "no";
        Subject subject = SecurityUtils.getSubject();
        UserBean user = service.findByName(userName);
        req.getSession().setAttribute("loginUser", user);
        try {
            //登录验证
            subject.login(new UsernamePasswordToken(userName, pwd));
        } catch (Exception e) {
            return "no";
        }
        return "ok";
    }

    @RequiresPermissions("grade:user")
    @RequestMapping("findRelName")
    @ApiOperation(value = "查看登录用户的真实姓名", notes = "查看登录用户的真实姓名")
    private String findRelName(HttpServletRequest req) {
        UserBean user = (UserBean) req.getSession().getAttribute("loginUser");
        return user.getRelName();
    }

    @RequiresPermissions("grade:user")
    @RequestMapping("cutByItem")
    @ApiOperation(value = "用户分页", notes = "分页展示用户")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNO", value = "页码", required = true),
            @ApiImplicitParam(name = "userName", value = "用户名")
    })
    public IPage<UserBean> cutByItem(Integer pageNO, String userName) {
        return service.cutByItem(pageNO, userName);
    }

    @RequiresPermissions("grade:user")
    @RequestMapping("del")
    @ApiOperation(value = "删除用户", notes = "删除用户")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userId", value = "用户id")
    })
    public String del(Integer userId) {
        service.del(userId);
        return "ok";
    }

    @RequiresPermissions("grade:user")
    @RequestMapping("findById")
    @ApiOperation(value = "查看用户", notes = "查看用户")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userId", value = "用户id")
    })
    public UserBean findById(Integer userId) {
        return service.findByUserId(userId);
    }

    @RequiresPermissions("grade:user")
    @RequestMapping("updateGrade")
    @ApiOperation(value = "改变用户的权限", notes = "改变用户的权限")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userId", value = "用户id"),
            @ApiImplicitParam(name = "newRoleIdArray", value = "新的角色id数组")
    })
    public String updateGrade(Integer userId, Integer[] newRoleIdArray) {
        service.updateRole(userId, newRoleIdArray);
        return "ok";
    }

    @RequiresPermissions("grade:user")
    @RequestMapping("add")
    @ApiOperation(value = "添加用户", notes = "添加用户")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "userName", value = "用户名"),
            @ApiImplicitParam(name = "relName", value = "用户真实姓名"),
            @ApiImplicitParam(name = "roleIdArray", value = "角色id数组")
    })
    public String add(String userName, String relName, Integer[] roleIdArray) {
//        service.add(new UserBean(userName, relName), roleIdArray);
        service.addUser(new UserBean(userName, relName), roleIdArray);
        return "ok";
    }

    @RequiresPermissions("grade:user")
    @RequestMapping("updatePwd")
    @ApiOperation(value = "修改密码", notes = "修改密码")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "newPwd", value = "新密码")
    })
    public String updatePwd(String newPwd, HttpServletRequest req) {
        UserBean user = (UserBean) req.getSession().getAttribute("loginUser");
        service.updatePwd(user.getUserId(), newPwd);
        return "ok";
    }

    @RequiresPermissions("grade:user")
    @RequestMapping("getUserInfo")
    @ApiOperation(value = "得到登录用户信息", notes = "得到登录用户信息")
    public UserBean getUserInfo(HttpServletRequest req) {
        UserBean user = (UserBean) req.getSession().getAttribute("loginUser");
        return user;
    }

    @ApiIgnore
    @RequestMapping("loginError")
    public String loginError() {
        return "请先登录";
    }
}
